package com.hq.ims.data.service.other;

import com.alibaba.fastjson.JSONObject;
import com.hq.ims.data.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;

/**
 * @author yinzhangxiao
 * @date 2024/11/13 10:16
 * @ClassName BaiduMapService
 **/
@Service
public class BaiduMapService {

    @Value("${userAttribute.baiduMapApi}")
    private String baiduMapUrl;

    @Value("${userAttribute.baiduMapAk}")
    private String baiduMapAk;

    /**
     * @description: 根据城市和关键字查询地图信息
     * @author: yinzhangxiao
     * @date: 2024/11/15 15:32
     */
    public Object getPlace(String keyWord, String region, String location) throws Exception {
        String url = baiduMapUrl + "/place/v2/suggestion?output=json" +
                "&query=" +
                URLEncoder.encode(keyWord, "UTF-8") +
                "&region=" +
                URLEncoder.encode(region, "UTF-8") +
                "&ak=" +
                URLEncoder.encode(baiduMapAk, "UTF-8") +
                "&location=" +
                URLEncoder.encode(location, "UTF-8");
        return JSONObject.parse(HttpClientUtil.doGet(url));
    }

    /**
     * @description: 根据地址查询经纬度（城市非必填）
     */
    public Object getPlaceByAddress(String address, String city) throws Exception {
        String url = baiduMapUrl + "/geocoding/v3/?output=json" +
                "&address=" +
                URLEncoder.encode(address, "UTF-8") +
                "&ak=" +
                URLEncoder.encode(baiduMapAk, "UTF-8") +
                "&city=" +
                URLEncoder.encode(city, "UTF-8");
        return JSONObject.parse(HttpClientUtil.doGet(url));
    }

    /**
     * @description: 根据经纬度返回详细地址
     */
    public Object getPlaceByLocation(String lng, String lat) throws Exception {
        String url = baiduMapUrl + "/reverse_geocoding/v3/?output=json&coordtype=wgs84ll&extensions_poi=1" +
                "&location=" +
                URLEncoder.encode(lat + "," + lng, "UTF-8") +
                "&ak=" +
                URLEncoder.encode(baiduMapAk, "UTF-8");
        return JSONObject.parse(HttpClientUtil.doGet(url));
    }
}
